//
//  CustomSegmentView.m
//  MaiJianBaseProject
//
//  Created by Michael Fan on 14-7-11.
//  Copyright (c) 2014年 Michael Fan. All rights reserved.
//

#import "CustomSegmentView.h"

@implementation CustomSegmentView

@synthesize selectedIndex;
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    //默认颜色
    self.normalColor = [UIColor grayColor];
    self.highlightedColor = [UIColor whiteColor];
    btn01.adjustsImageWhenHighlighted = NO;
    btn02.adjustsImageWhenHighlighted = NO;
    if (btn03) {
        btn03.adjustsImageWhenHighlighted = NO;
    }
    if (btn04) {
        btn04.adjustsImageWhenHighlighted = NO;
    }
    if (btn05) {
        btn05.adjustsImageWhenHighlighted = NO;
    }
    
    if (self.imageOrBackgroundImage) {
        [btn01 setImage:[btn01 imageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
        [btn01 setImage:[btn01 imageForState:UIControlStateSelected] forState:UIControlStateSelected];
        [btn02 setImage:[btn02 imageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
        [btn02 setImage:[btn02 imageForState:UIControlStateSelected] forState:UIControlStateSelected];
        if (btn03) {
            [btn03 setImage:[btn03 imageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
            [btn03 setImage:[btn03 imageForState:UIControlStateSelected] forState:UIControlStateSelected];
        }
        if (btn04) {
            [btn04 setImage:[btn04 imageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
            [btn04 setImage:[btn04 imageForState:UIControlStateSelected] forState:UIControlStateSelected];
        }
        if (btn05) {
            [btn05 setImage:[btn05 imageForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
            [btn05 setImage:[btn05 imageForState:UIControlStateSelected] forState:UIControlStateSelected];
        }
    }
    else
    {
        [btn01 setBackgroundImage:[[btn01 backgroundImageForState:UIControlStateHighlighted] stretchableImageWithLeftCapWidth:8 topCapHeight:10] forState:UIControlStateHighlighted];
        [btn01 setBackgroundImage:[[btn01 backgroundImageForState:UIControlStateSelected] stretchableImageWithLeftCapWidth:8 topCapHeight:10] forState:UIControlStateSelected];
        [btn02 setBackgroundImage:[[btn02 backgroundImageForState:UIControlStateHighlighted] stretchableImageWithLeftCapWidth:8 topCapHeight:10] forState:UIControlStateHighlighted];
        [btn02 setBackgroundImage:[[btn02 backgroundImageForState:UIControlStateSelected] stretchableImageWithLeftCapWidth:8 topCapHeight:10] forState:UIControlStateSelected];
        if (btn03) {
            [btn03 setBackgroundImage:[[btn03 backgroundImageForState:UIControlStateHighlighted] stretchableImageWithLeftCapWidth:8 topCapHeight:10] forState:UIControlStateHighlighted];
            [btn03 setBackgroundImage:[[btn03 backgroundImageForState:UIControlStateSelected] stretchableImageWithLeftCapWidth:8 topCapHeight:10] forState:UIControlStateSelected];
        }
        if (btn04) {
            [btn04 setBackgroundImage:[[btn04 backgroundImageForState:UIControlStateHighlighted] stretchableImageWithLeftCapWidth:8 topCapHeight:10] forState:UIControlStateHighlighted];
            [btn04 setBackgroundImage:[[btn04 backgroundImageForState:UIControlStateSelected] stretchableImageWithLeftCapWidth:8 topCapHeight:10] forState:UIControlStateSelected];
        }
        if (btn05) {
            [btn05 setBackgroundImage:[[btn05 backgroundImageForState:UIControlStateHighlighted] stretchableImageWithLeftCapWidth:8 topCapHeight:10] forState:UIControlStateHighlighted];
            [btn05 setBackgroundImage:[[btn05 backgroundImageForState:UIControlStateSelected] stretchableImageWithLeftCapWidth:8 topCapHeight:10] forState:UIControlStateSelected];
        }
    }
    
    [self btnSelected:btn01];
}

-(void)setImageOrBackgroundImage:(BOOL)imageOrBackgroundImagea
{
    _imageOrBackgroundImage = imageOrBackgroundImagea;
    if (_imageOrBackgroundImage)
    {
        UIButton *btnTag = (UIButton *)[self viewWithTag:self.selectedIndex];
        
        if (btnTag == btn01) {
            
            [btn01 setImage:[btn01 imageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
            [btn02 setImage:[btn02 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            if (btn03) {
                [btn03 setImage:[btn03 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            }
            if (btn04) {
                [btn04 setImage:[btn04 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                
            }
            if (btn05) {
                [btn05 setImage:[btn05 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                
            }
        }
        //新闻
        else if(btnTag == btn02)
        {
            
            [btn02 setImage:[btn02 imageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
            [btn01 setImage:[btn01 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            if (btn03) {
                [btn03 setImage:[btn03 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                
            }
            if (btn04) {
                [btn04 setImage:[btn04 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            }
            if (btn05) {
                [btn05 setImage:[btn05 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            }
            
        }
        else if(btnTag == btn03)
        {
            
            [btn01 setImage:[btn01 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn02 setImage:[btn02 imageForState:UIControlStateSelected] forState:UIControlStateNormal];

            if (btn03) {
                [btn03 setImage:[btn03 imageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
            }
            if (btn04) {
                [btn04 setImage:[btn04 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            }
            if (btn05) {
                [btn05 setImage:[btn05 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            }
        }
        else if(btnTag == btn04)
        {
            
            [btn01 setImage:[btn01 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
        
            [btn02 setImage:[btn02 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
        
            if (btn03) {
                [btn03 setImage:[btn03 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
        
            }
            if (btn04) {
                [btn04 setImage:[btn04 imageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
            }
            if (btn05) {
                [btn05 setImage:[btn05 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            }
        }
        else if(btnTag == btn05)
        {
            
            [btn01 setImage:[btn01 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            
            [btn02 setImage:[btn02 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            
            if (btn03) {
                [btn03 setImage:[btn03 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                
            }
            if (btn04) {
                [btn04 setImage:[btn04 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            }
            if (btn05) {
                [btn05 setImage:[btn05 imageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
            }
        }
    }
}

-(void)updateTextColor
{
    UIButton *btnTag = (UIButton *)[self viewWithTag:self.selectedIndex];
    
    if (btnTag == btn01)
    {
        
        [btn01 setTitleColor:self.highlightedColor forState:UIControlStateNormal];
        [btn01 setTitleColor:self.highlightedColor forState:UIControlStateHighlighted];
        
        [btn02 setTitleColor:self.normalColor forState:UIControlStateNormal];
        [btn02 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
        
        if (btn03) {
            
            [btn03 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn03 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
        }
        if (btn04)
        {
            [btn04 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn04 setTitleColor:self.normalColor forState:UIControlStateHighlighted];

        }
        if (btn05)
        {
            [btn05 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn05 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            
        }
    }
    //新闻
    else if(btnTag == btn02)
    {
        
        [btn02 setTitleColor:self.highlightedColor forState:UIControlStateNormal];
        [btn02 setTitleColor:self.highlightedColor forState:UIControlStateHighlighted];
        
        [btn01 setTitleColor:self.normalColor forState:UIControlStateNormal];
        [btn01 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
        if (btn03) {
            
            [btn03 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn03 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            
        }if (btn04)
        {
            [btn04 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn04 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            
        }
        if (btn05)
        {
            [btn05 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn05 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            
        }
        
    }
    else if(btnTag == btn03)
    {
        
        [btn01 setTitleColor:self.normalColor forState:UIControlStateNormal];
        [btn01 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
        
        [btn02 setTitleColor:self.normalColor forState:UIControlStateNormal];
        [btn02 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
        if (btn03) {
            
            [btn03 setTitleColor:self.highlightedColor forState:UIControlStateNormal];
            [btn03 setTitleColor:self.highlightedColor forState:UIControlStateHighlighted];
        }
        if (btn04)
        {
            [btn04 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn04 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            
        }
        if (btn05)
        {
            [btn05 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn05 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            
        }
    }
    else if(btnTag == btn04)
    {
        
        [btn01 setTitleColor:self.normalColor forState:UIControlStateNormal];
        [btn01 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
        
        [btn02 setTitleColor:self.normalColor forState:UIControlStateNormal];
        [btn02 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
        if (btn03)
        {
            [btn03 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn03 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            
        }
        if (btn04)
        {
            [btn04 setTitleColor:self.highlightedColor forState:UIControlStateNormal];
            [btn04 setTitleColor:self.highlightedColor forState:UIControlStateHighlighted];
        }
        if (btn05)
        {
            [btn05 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn05 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            
        }
    }
    else if(btnTag == btn05)
    {
        
        [btn01 setTitleColor:self.normalColor forState:UIControlStateNormal];
        [btn01 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
        
        [btn02 setTitleColor:self.normalColor forState:UIControlStateNormal];
        [btn02 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
        if (btn03)
        {
            [btn03 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn03 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            
        }
        if (btn04)
        {
            [btn04 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn04 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
        }
        if (btn05)
        {
            [btn05 setTitleColor:self.highlightedColor forState:UIControlStateNormal];
            [btn05 setTitleColor:self.highlightedColor forState:UIControlStateHighlighted];
            
        }
    }
}


-(void)viewBecomeToIndex:(NSInteger)toIndex
{
    if (self.imageOrBackgroundImage)
    {
        if (toIndex == btn01.tag) {
            [btn01 setImage:[btn01 imageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
            [btn01 setTitleColor:self.highlightedColor forState:UIControlStateNormal];
            [btn01 setTitleColor:self.highlightedColor forState:UIControlStateHighlighted];
            [btn02 setImage:[btn02 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            if (btn03) {
                [btn03 setImage:[btn03 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
            if (btn04) {
                [btn04 setImage:[btn04 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
            if (btn05) {
                [btn05 setImage:[btn05 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
        }
        //
        else if(toIndex == btn02.tag)
        {
            [btn02 setImage:[btn02 imageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
            [btn02 setTitleColor:self.highlightedColor forState:UIControlStateNormal];
            [btn02 setTitleColor:self.highlightedColor forState:UIControlStateHighlighted];
            [btn01 setImage:[btn01 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            if (btn03) {
                [btn03 setImage:[btn03 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
                
            } if (btn04) {
                [btn04 setImage:[btn04 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
            if (btn05) {
                [btn05 setImage:[btn05 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
            
        }
        else if(toIndex == btn03.tag)
        {
            [btn01 setImage:[btn01 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            [btn02 setImage:[btn02 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            if (btn03) {
                [btn03 setImage:[btn03 imageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
                [btn03 setTitleColor:self.highlightedColor forState:UIControlStateNormal];
                [btn03 setTitleColor:self.highlightedColor forState:UIControlStateHighlighted];
            } if (btn04) {
                [btn04 setImage:[btn04 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
            if (btn05) {
                [btn05 setImage:[btn05 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
            
        }
        else if(toIndex == btn04.tag)
        {
            [btn01 setImage:[btn01 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            [btn02 setImage:[btn02 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            if (btn03) {
                [btn03 setImage:[btn03 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            } if (btn04) {
                [btn04 setImage:[btn04 imageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
                [btn04 setTitleColor:self.highlightedColor forState:UIControlStateNormal];
                [btn04 setTitleColor:self.highlightedColor forState:UIControlStateHighlighted];
            }
            if (btn05) {
                [btn05 setImage:[btn05 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
            
        }
        else if(toIndex == btn04.tag)
        {
            [btn01 setImage:[btn01 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            [btn02 setImage:[btn02 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            if (btn03) {
                [btn03 setImage:[btn03 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            } if (btn04) {
                [btn04 setImage:[btn04 imageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
            if (btn05) {
                [btn05 setImage:[btn05 imageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
                [btn05 setTitleColor:self.highlightedColor forState:UIControlStateNormal];
                [btn05 setTitleColor:self.highlightedColor forState:UIControlStateHighlighted];
            }
            
        }
    }
    else
    {
        if (toIndex == btn01.tag) {
            [btn01 setBackgroundImage:[btn01 backgroundImageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
            [btn01 setTitleColor:self.highlightedColor forState:UIControlStateNormal];
            [btn01 setTitleColor:self.highlightedColor forState:UIControlStateHighlighted];
            [btn02 setBackgroundImage:[btn02 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            if (btn03) {
                [btn03 setBackgroundImage:[btn03 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
            if (btn04) {
                [btn04 setBackgroundImage:[btn04 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
            if (btn05) {
                [btn05 setBackgroundImage:[btn05 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
        }
        //
        else if(toIndex == btn02.tag)
        {
            [btn02 setBackgroundImage:[btn02 backgroundImageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
            [btn02 setTitleColor:self.highlightedColor forState:UIControlStateNormal];
            [btn02 setTitleColor:self.highlightedColor forState:UIControlStateHighlighted];
            [btn01 setBackgroundImage:[btn01 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            if (btn03) {
                [btn03 setBackgroundImage:[btn03 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
                
            } if (btn04) {
                [btn04 setBackgroundImage:[btn04 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
            if (btn05) {
                [btn05 setBackgroundImage:[btn05 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
            
        }
        else if(toIndex == btn03.tag)
        {
            [btn01 setBackgroundImage:[btn01 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            [btn02 setBackgroundImage:[btn02 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            if (btn03) {
                [btn03 setBackgroundImage:[btn03 backgroundImageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
                [btn03 setTitleColor:self.highlightedColor forState:UIControlStateNormal];
                [btn03 setTitleColor:self.highlightedColor forState:UIControlStateHighlighted];
            } if (btn04) {
                [btn04 setBackgroundImage:[btn04 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
            if (btn05) {
                [btn05 setBackgroundImage:[btn05 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
            
        }
        else if(toIndex == btn04.tag)
        {
            [btn01 setBackgroundImage:[btn01 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            [btn02 setBackgroundImage:[btn02 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            if (btn03) {
                [btn03 setBackgroundImage:[btn03 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            } if (btn04) {
                [btn04 setBackgroundImage:[btn04 backgroundImageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
                [btn04 setTitleColor:self.highlightedColor forState:UIControlStateNormal];
                [btn04 setTitleColor:self.highlightedColor forState:UIControlStateHighlighted];
            }
            if (btn05) {
                [btn05 setBackgroundImage:[btn05 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn05 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
        }
        else if(toIndex == btn05.tag)
        {
            [btn01 setBackgroundImage:[btn01 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn01 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            [btn02 setBackgroundImage:[btn02 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateNormal];
            [btn02 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            if (btn03) {
                [btn03 setBackgroundImage:[btn03 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn03 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            } if (btn04) {
                [btn04 setBackgroundImage:[btn04 backgroundImageForState:UIControlStateSelected] forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateNormal];
                [btn04 setTitleColor:self.normalColor forState:UIControlStateHighlighted];
            }
            if (btn05) {
                [btn05 setBackgroundImage:[btn05 backgroundImageForState:UIControlStateHighlighted] forState:UIControlStateNormal];
                [btn05 setTitleColor:self.highlightedColor forState:UIControlStateNormal];
                [btn05 setTitleColor:self.highlightedColor forState:UIControlStateHighlighted];
            }
        }
    }
}

-(void)setSelectedIndex:(int)aselectedIndex
{
    //界面相应处理
    if ([[self viewWithTag:aselectedIndex] isKindOfClass:[UIButton class]])
    {
        [self btnSelected:[self viewWithTag:aselectedIndex]];
    }
}

-(IBAction)btnSelected:(id)sender
{
    if (self.toScroller)
    {
        if (self.toScroller.decelerating)
        {
            return;
        }
    }
    
    UIButton *btnSender = (UIButton *)sender;
    if (btnSender.tag == self.selectedIndex)
    {
        if ([self.delegate respondsToSelector:@selector(customSegDidTapSelectedItem:)])
        {
            [self.delegate customSegDidTapSelectedItem:self];
        }
        return;
    }
    
    [self viewBecomeToIndex:btnSender.tag];
    
    selectedIndex = btnSender.tag;
    if ([self.delegate respondsToSelector:@selector(customSegDidSelected:)])
    {
        [self.delegate customSegDidSelected:self];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
